import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:sticky_headers/sticky_headers/widget.dart';
import 'package:yi_shop/model/brand.dart';
import 'package:yi_shop/route/index.dart';
import 'package:yi_shop/ui/scroll_refresh_config.dart';
import 'package:yi_shop/utils/colors.dart';
import 'package:yi_shop/utils/styles.dart';
import 'package:yi_shop/utils/svg.dart';
import 'package:yi_shop/utils/tools.dart';

class BrandZoneController extends GetxController {
  RxList<BrandClassifyModel> pageList = RxList([]);

  RefreshController refreshController = RefreshController();

  @override
  onInit() {
    initData();
    super.onInit();
  }

  Future<void> onRefresh() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));
    refreshController.refreshCompleted(resetFooterState: true);
  }

  Future<void> onLoading() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));

    /// if failed, use loadFailed(), if no data return, use LoadNodata()
    if (pageList.length > 5) {
      return refreshController.loadNoData();
    }

    /// 添加数据
    initData();

    /// 完毕
    refreshController.loadComplete();
  }

  void initData() {
    pageList.add(BrandClassifyModel(name: '电脑办公', children: [
      BrandModel(
          name: '华为',
          imgUrl:
              'https://img.alicdn.com/img/i3/113221094/O1CN01FrNB2V1Jx6EUWND6k_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '荣耀',
          imgUrl:
              'https://img.alicdn.com/img/i2/34403627/O1CN0196aIrL1cfDT8W4cZ5_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '小米',
          imgUrl:
              'https://img.alicdn.com/img/i4/13491845/O1CN01iuHe7S1PV3g8fNQtR_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '华硕',
          imgUrl:
              'https://img.alicdn.com/img/i3/111683677/O1CN01XTLeBT1d27Gq5lkwu_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '宏基',
          imgUrl:
              'https://img.alicdn.com/img/i1/111683677/O1CN014zByJ51d27Gq5UIw0_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '联想',
          imgUrl:
              'https://img.alicdn.com/img/i3/117080183/O1CN01mqX3at1DDrPuhm8iy_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '惠普',
          imgUrl:
              'https://img.alicdn.com/img/i4/13187539/O1CN01AhM7Sg25YusVFkreQ_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '戴尔',
          imgUrl:
              'https://img.alicdn.com/img/i1/4252860039/O1CN01uOuM4a1C9uMQ0k2yF_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '三星',
          imgUrl:
              'https://img.alicdn.com/imghttps://img.alicdn.com/imgextra/i4/2207907575309/O1CN01h2zklq1p5ZdQpK463_!!2207907575309-0-alimamacc.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: 'LG',
          imgUrl:
              'https://img.alicdn.com/img/i1/1487820114/O1CN01cdH39S1CiG4RhFFyV_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '苹果',
          imgUrl:
              'https://img.alicdn.com/img/i4/195765460/O1CN01KGinY81qCjPnhzuy2_!!195765460-0-alimamacc.jpg_360x360xzq75.jpg_.webp'),
    ]));
    pageList.add(BrandClassifyModel(name: '手机数码', children: [
      BrandModel(
          name: '华为',
          imgUrl:
              'https://img.alicdn.com/img/i3/113221094/O1CN01FrNB2V1Jx6EUWND6k_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '荣耀',
          imgUrl:
              'https://img.alicdn.com/img/i2/34403627/O1CN0196aIrL1cfDT8W4cZ5_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '小米',
          imgUrl:
              'https://img.alicdn.com/img/i4/13491845/O1CN01iuHe7S1PV3g8fNQtR_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '华硕',
          imgUrl:
              'https://img.alicdn.com/img/i3/111683677/O1CN01XTLeBT1d27Gq5lkwu_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '宏基',
          imgUrl:
              'https://img.alicdn.com/img/i1/111683677/O1CN014zByJ51d27Gq5UIw0_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '联想',
          imgUrl:
              'https://img.alicdn.com/img/i3/117080183/O1CN01mqX3at1DDrPuhm8iy_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '惠普',
          imgUrl:
              'https://img.alicdn.com/img/i4/13187539/O1CN01AhM7Sg25YusVFkreQ_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '戴尔',
          imgUrl:
              'https://img.alicdn.com/img/i1/4252860039/O1CN01uOuM4a1C9uMQ0k2yF_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '三星',
          imgUrl:
              'https://img.alicdn.com/imghttps://img.alicdn.com/imgextra/i4/2207907575309/O1CN01h2zklq1p5ZdQpK463_!!2207907575309-0-alimamacc.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: 'LG',
          imgUrl:
              'https://img.alicdn.com/img/i1/1487820114/O1CN01cdH39S1CiG4RhFFyV_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '苹果',
          imgUrl:
              'https://img.alicdn.com/img/i4/195765460/O1CN01KGinY81qCjPnhzuy2_!!195765460-0-alimamacc.jpg_360x360xzq75.jpg_.webp'),
    ]));
    pageList.add(BrandClassifyModel(name: '家用电器', children: [
      BrandModel(
          name: '飞利浦',
          imgUrl:
              'https://img.alicdn.com/img/i3/4913528863/O1CN019Gj2dO2FLJCfhYWbs_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '海尔',
          imgUrl:
              'https://img.alicdn.com/img/i2/34403627/O1CN0196aIrL1cfDT8W4cZ5_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '海信',
          imgUrl:
              'https://img.alicdn.com/img/i4/13491845/O1CN01iuHe7S1PV3g8fNQtR_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '创维',
          imgUrl:
              'https://img.alicdn.com/img/i4/1734980071/O1CN01cOv9Jv1COYx0jidGi_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '宏基',
          imgUrl:
              'https://img.alicdn.com/img/i1/111683677/O1CN014zByJ51d27Gq5UIw0_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: 'TCL',
          imgUrl:
              'https://img.alicdn.com/img/i3/117080183/O1CN01mqX3at1DDrPuhm8iy_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '美的',
          imgUrl:
              'https://img.alicdn.com/img/i4/2211793943755/O1CN01OkLKQe1dbq32eA64D_!!2211793943755-0-alimamacc.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '格力',
          imgUrl:
              'https://img.alicdn.com/img/i2/121553216/O1CN01A5yI691ZcyYMrfofb_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '九阳',
          imgUrl:
              'https://img.alicdn.com/img/i1/4059001418/O1CN01vYxiQs1MLUYghw6HN_!!4059001418-0-alimamacc.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '苏泊尔',
          imgUrl:
              'https://img.alicdn.com/img/i3/131902852/O1CN014zoGzM1WwGTWwfx4X_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '方太',
          imgUrl:
              'https://img.alicdn.com/img/i2/123946349/O1CN01P0XKl51wltYkzCNiI_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
    ]));
    pageList.add(BrandClassifyModel(name: '服装饰品', children: [
      BrandModel(
          name: '九牧王',
          imgUrl:
              'https://img.alicdn.com/img/i3/4913528863/O1CN019Gj2dO2FLJCfhYWbs_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '红豆',
          imgUrl:
              'https://img.alicdn.com/img/i2/34403627/O1CN0196aIrL1cfDT8W4cZ5_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '柒牌',
          imgUrl:
              'https://img.alicdn.com/img/i4/13491845/O1CN01iuHe7S1PV3g8fNQtR_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '真维斯',
          imgUrl:
              'https://img.alicdn.com/img/i4/1734980071/O1CN01cOv9Jv1COYx0jidGi_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '七匹狼',
          imgUrl:
              'https://img.alicdn.com/img/i1/111683677/O1CN014zByJ51d27Gq5UIw0_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '贵人鸟',
          imgUrl:
              'https://img.alicdn.com/img/i3/117080183/O1CN01mqX3at1DDrPuhm8iy_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '美特斯邦威',
          imgUrl:
              'https://img.alicdn.com/img/i4/2211793943755/O1CN01OkLKQe1dbq32eA64D_!!2211793943755-0-alimamacc.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '森马',
          imgUrl:
              'https://img.alicdn.com/img/i2/121553216/O1CN01A5yI691ZcyYMrfofb_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '啄木鸟',
          imgUrl:
              'https://img.alicdn.com/img/i1/4059001418/O1CN01vYxiQs1MLUYghw6HN_!!4059001418-0-alimamacc.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '花花公子',
          imgUrl:
              'https://img.alicdn.com/img/i3/131902852/O1CN014zoGzM1WwGTWwfx4X_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '回力',
          imgUrl:
              'https://img.alicdn.com/img/i2/123946349/O1CN01P0XKl51wltYkzCNiI_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '海澜之家',
          imgUrl:
              'https://img.alicdn.com/img/i4/2211793943755/O1CN01OkLKQe1dbq32eA64D_!!2211793943755-0-alimamacc.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '罗蒙',
          imgUrl:
              'https://img.alicdn.com/img/i2/121553216/O1CN01A5yI691ZcyYMrfofb_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '太平鸟',
          imgUrl:
              'https://img.alicdn.com/img/i1/4059001418/O1CN01vYxiQs1MLUYghw6HN_!!4059001418-0-alimamacc.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '伊芙丽',
          imgUrl:
              'https://img.alicdn.com/img/i3/131902852/O1CN014zoGzM1WwGTWwfx4X_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '吉普',
          imgUrl:
              'https://img.alicdn.com/img/i2/123946349/O1CN01P0XKl51wltYkzCNiI_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
    ]));
    pageList.add(BrandClassifyModel(name: '户外运动', children: [
      BrandModel(
          name: '安踏',
          imgUrl:
              'https://img.alicdn.com/img/i3/4913528863/O1CN019Gj2dO2FLJCfhYWbs_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '乔丹',
          imgUrl:
              'https://img.alicdn.com/img/i2/34403627/O1CN0196aIrL1cfDT8W4cZ5_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '鸿星尔克',
          imgUrl:
              'https://img.alicdn.com/img/i4/13491845/O1CN01iuHe7S1PV3g8fNQtR_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '木森林',
          imgUrl:
              'https://img.alicdn.com/img/i4/1734980071/O1CN01cOv9Jv1COYx0jidGi_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '卡梵森',
          imgUrl:
              'https://img.alicdn.com/img/i1/111683677/O1CN014zByJ51d27Gq5UIw0_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '361',
          imgUrl:
              'https://img.alicdn.com/img/i3/117080183/O1CN01mqX3at1DDrPuhm8iy_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '特莱尔',
          imgUrl:
              'https://img.alicdn.com/img/i4/2211793943755/O1CN01OkLKQe1dbq32eA64D_!!2211793943755-0-alimamacc.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '三福',
          imgUrl:
              'https://img.alicdn.com/img/i2/121553216/O1CN01A5yI691ZcyYMrfofb_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '斯雷威',
          imgUrl:
              'https://img.alicdn.com/img/i1/4059001418/O1CN01vYxiQs1MLUYghw6HN_!!4059001418-0-alimamacc.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '骆驼',
          imgUrl:
              'https://img.alicdn.com/img/i3/131902852/O1CN014zoGzM1WwGTWwfx4X_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '南极人',
          imgUrl:
              'https://img.alicdn.com/img/i2/123946349/O1CN01P0XKl51wltYkzCNiI_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '亚瑟士',
          imgUrl:
              'https://img.alicdn.com/img/i4/2211793943755/O1CN01OkLKQe1dbq32eA64D_!!2211793943755-0-alimamacc.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '迪卡侬',
          imgUrl:
              'https://img.alicdn.com/img/i2/121553216/O1CN01A5yI691ZcyYMrfofb_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '耐克',
          imgUrl:
              'https://img.alicdn.com/img/i1/4059001418/O1CN01vYxiQs1MLUYghw6HN_!!4059001418-0-alimamacc.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '阿迪达斯',
          imgUrl:
              'https://img.alicdn.com/img/i3/131902852/O1CN014zoGzM1WwGTWwfx4X_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
      BrandModel(
          name: '伯希和',
          imgUrl:
              'https://img.alicdn.com/img/i2/123946349/O1CN01P0XKl51wltYkzCNiI_!!0-saturn_solar.jpg_360x360xzq75.jpg_.webp'),
    ]));
  }
}

class BrandZonePage extends GetView<BrandZoneController> {
  const BrandZonePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: ToolsUtils.builderLeading(),
        title: const Text('全部品牌'),
      ),
      body: Obx(() => scrollRefreshConfig(context)),
    );
  }

  ScrollRefreshConfig scrollRefreshConfig(BuildContext context) {
    return ScrollRefreshConfig(
      refreshController: controller.refreshController,
      onRefresh: controller.onRefresh,
      onLoading: controller.onLoading,
      child: listViewBuilder(context),
    );
  }

  ListView listViewBuilder(BuildContext context) {
    return ListView.builder(
      padding: EdgeInsets.only(top: 15.h),
      itemBuilder: (_, int index) {
        var row = controller.pageList[index];
        return StickyHeader(
          header: Container(
            height: 45,
            decoration: const BoxDecoration(color: Colors.white),
            padding: EdgeInsets.symmetric(horizontal: 20.w),
            alignment: Alignment.centerLeft,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  row.name,
                  style: StylesUtils.customTextStyle(
                      color: ColorsUtils.color3333,
                      fontWeight: FontWeight.w700,
                      fontSize: StylesUtils.fontSize13),
                ),
                TextButton(
                    onPressed: () {
                      Get.toNamed(RoutePath.brandClassify,
                          arguments: {'name': row.name, 'children': row.children});
                    },
                    style: const ButtonStyle(alignment: Alignment.center),
                    child: Text.rich(TextSpan(children: [
                      TextSpan(
                          text: '更多 ',
                          style: StylesUtils.customTextStyle(color: ColorsUtils.color9999)),
                      WidgetSpan(
                          child: SvgUtils.assetName('u4623', width: 20, height: 20),
                          alignment: PlaceholderAlignment.middle)
                    ]))),
              ],
            ),
          ),
          content: Container(
            padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 10.h),
            color: Colors.white,
            child: StaggeredGrid.count(
              crossAxisCount: 4,
              crossAxisSpacing: 10,
              mainAxisSpacing: 15,
              children: row.children
                  .map((e) => InkWell(
                        onTap: () {
                          Get.toNamed(RoutePath.brandInfo, arguments: {'row': e});
                        },
                        child: Column(
                          children: [
                            Center(
                              child: ClipRRect(
                                borderRadius: BorderRadius.circular(5.r),
                                child: CachedNetworkImage(
                                  imageUrl: e.imgUrl,
                                  width: 75,
                                  height: 60,
                                  fit: BoxFit.fill,
                                ),
                              ),
                            ),
                            Container(
                              margin: EdgeInsets.only(top: 5.h),
                              alignment: Alignment.center,
                              child: Text(
                                e.name,
                                style: StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                              ),
                            )
                          ],
                        ),
                      ))
                  .toList(),
            ),
          ),
        );
      },
      itemCount: controller.pageList.length,
    );
  }
}
